//
//  FinishViewController.m
//  51易借
//
//  Created by 张睿 on 2017/8/17.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "FinishViewController.h"
#import "AppDelegate.h"
#import "MainViewController.h"
#import "FinishInfoViewController.h"

@interface FinishViewController ()


@property (nonatomic, weak)UIImageView * topView;
@property (nonatomic, weak)UIImageView * successImageView;
@property (nonatomic, weak)UILabel * successLabel;
@property (nonatomic, weak)UILabel * explationLabel;
@property (nonatomic, weak)UIButton * approveButton;
@property (nonatomic, weak)UIButton * watchButton;


@end

@implementation FinishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self login];
    [self setupNavBar];
    [self setupUI];
}

- (void)login{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary * parameters = @{@"userPhone":self.telString,@"password":self.password};
    
    [[NetworkTool shareManager] POST:@"user/login.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            
            DEF_SET_OBJECT([[responseObject objectForKey:@"data"] objectForKey:@"id"],userID);
            DEF_SET_OBJECT([[responseObject objectForKey:@"data"] objectForKey:@"token"],userToken);
            DEF_SET_OBJECT( [[responseObject objectForKey:@"data"] objectForKey:@"account"],myAccount);
            
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
//        NSLog(@"==%@",error);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)setupNavBar{
    
    self.title = @"注册成功";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UIImageView * topView = [UIImageView new];
    
    topView.backgroundColor = ColorWithRGB(0xff7907);
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    
    //注册成功
    UIImageView * successImageView = [UIImageView new];
    
    successImageView.image = [UIImage imageNamed:@"注册成功"];
    
    self.successImageView = successImageView;
    
    [self.view addSubview:self.successImageView];
    
    
    UILabel * successLabel = [UILabel new];
    
    successLabel.text = @"注册成功";
    successLabel.textColor = ColorWithRGB(0x36353b);
    successLabel.textAlignment = NSTextAlignmentCenter;
    successLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
    
    self.successLabel = successLabel;
    
    [self.view addSubview:self.successLabel];
    
    
    UILabel * explationLabel = [UILabel new];
    
    explationLabel.text = @"恭喜你已经注册成功，赶快完成认证查看自己的信用额度吧！";
    explationLabel.textColor = ColorWithRGB(0xfe6a05);
    explationLabel.textAlignment = NSTextAlignmentCenter;
    explationLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
    
    self.explationLabel = explationLabel;
    
    [self.view addSubview:self.explationLabel];
    
    
    UIButton * approveButton = [UIButton new];
    //300 * 42
    [approveButton setTitle:@"去认证" forState:UIControlStateNormal];
    [approveButton setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
    [approveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [approveButton addTarget:self action:@selector(didApproveButton) forControlEvents:UIControlEventTouchUpInside];
    approveButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.approveButton = approveButton;
    
    [self.view addSubview:self.approveButton];
    
    
    UIButton * watchButton = [UIButton new];
    
    [watchButton setTitle:@"随便看看" forState:UIControlStateNormal];
    [watchButton setTitleColor:ColorWithRGB(0xb4b5b6) forState:UIControlStateNormal];
    [watchButton addTarget:self action:@selector(didWatchButton) forControlEvents:UIControlEventTouchUpInside];
    watchButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.watchButton = watchButton;
    
    [self.view addSubview:self.watchButton];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    
    [self.successImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.height.mas_equalTo(95 * SCREENH6);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(38 * SCREENH6);
    }];
   
    [self.successLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.successImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.explationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.successLabel.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.approveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(300 * SCREENH6);
        make.height.mas_equalTo(42 * SCREENH6);
        make.top.mas_equalTo(self.explationLabel.mas_bottom).offset(37 * SCREENH6);
    }];
    
    [self.watchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.approveButton.mas_bottom).offset(37 * SCREENH6);
    }];
}


//点击去认证按钮
- (void)didApproveButton{
    
    FinishInfoViewController * finishVC = [FinishInfoViewController new];
    finishVC.setUp = @"setUp";
    
    [self.navigationController pushViewController:finishVC animated:YES];
}


//点击随便看看按钮
- (void)didWatchButton{
    
//    NSDictionary * parameters = @{@"userPhone":self.telString,@"password":self.password};
//    
//    [[NetworkTool shareManager] POST:@"user/login.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
//        
//        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
//        
//        if ([code isEqualToString:@"0"]) {
//            
//            DEF_SET_OBJECT([[responseObject objectForKey:@"data"] objectForKey:@"id"],userID);
//            DEF_SET_OBJECT([[responseObject objectForKey:@"data"] objectForKey:@"token"],userToken);
//            DEF_SET_OBJECT( [[responseObject objectForKey:@"data"] objectForKey:@"account"],myAccount);
            
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            
            MainViewController *tabs = [[MainViewController alloc] init];
            
            appDelegate.window.rootViewController = tabs;
            
            
//        }else{
//            
//            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
//                                                                           message:responseObject[@"message"]
//                                                                    preferredStyle:UIAlertControllerStyleAlert];
//            
//            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
//                                                                  handler:^(UIAlertAction * action) {
//                                                                      //响应事件
//                                                                      
//                                                                  }];
//            
//            [alert addAction:defaultAction];
//            [self presentViewController:alert animated:YES completion:nil];
//        }
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        NSLog(@"==%@",error);
//        
//    }];
    
}


- (void)backToLastView{
    
//    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
